//index.js

var num
var app= getApp();
import http from "../../../class/Utils/Http"
import tips from "../../../class/Utils/Tips"
import user from "../../../class/Utils/User"
Page({
    data: {
        maxlengthshu: '-1',//输入文本的长度限制，-1是无限制
        leixingdata: ['','求购', '供应', '招商', '商服'],
        leixingindex: 1,
        hangyeindex: 0,
        img_arr:[],
        pic1:'',
        pic2:'',
        pic3:'',
    },

    onLoad: function () {
        var that = this
        var user = wx.getStorageSync('user')
        tips.loading("加载中。。")
        http.req("/Api/shangji_trade",{uid:user.userid},function (res) {
            tips.loaded()
            that.setData({
                info:res,
                info_list:res.info_list
            })
        })
    },
    //选择类型
    bindleixingChange: function (e) {
        var that = this
        that.setData({
            leixingindex: e.detail.value
        })
    },
    //选择行业
    bindhangyeChange: function (e) {
        var that = this
        that.setData({
            hangyeindex: e.detail.value
        })
    },
    upimg: function () {
        var that = this;
        var img_length = that.data.img_arr.length;
        wx.chooseImage({
            sizeType: ['original', 'compressed'],
            success: function (res) {
                console.log(res)
                num = res.tempFilePaths.length + img_length
                if (num > 3) {
                    wx.showModal({
                        title: '温馨提示',
                        content: '最多上传3张图片',
                        showCancel: false
                    })
                    return false
                }
                that.setData({
                    img_arr: that.data.img_arr.concat(res.tempFilePaths),
                })

            }
        })
    },
    xxx: function (e) {
        var that = this
        var index = e.currentTarget.dataset.index
        var img_arr = that.data.img_arr;
        img_arr.splice(index, 1);
        this.setData({
            img_arr: img_arr
        });
    },
    /*
    选择 圈
     */
    xz_quan:function (e) {
       var that = this
        var index = e.currentTarget.dataset.index
        var list = that.data.info_list
        list[index].select = !list[index].select
        that.setData({
            info_list:list
        })
    },
    up: function (cb) {

        var that = this;
        console.log(that.data.img_arr.length)
        var i = that.data.i?that.data.i:0
        var j = i+1
        var rootDocment = app.httpurl.url
        wx.uploadFile({
            url: rootDocment + '/Api/shangji_pic',
            filePath: that.data.img_arr[i],
            name: 'pic', //文件对应的参数名字(key)
            success: function (suc) {
            }, 
            complete: function (complete) {
                console.log(complete);
                console.log("处理多图第"+i+"张");
                i++
                var data = JSON.parse(complete.data)
                console.log(data);
                var pic = data.pic.savepath + data.pic.savename

                if(j==1){
                    that.setData({i:i,pic1:pic})
                }
                if(j==2){
                    that.setData({i:i,pic2:pic})
                }
                if(j==3){
                    that.setData({i:i,pic3:pic})
                }
                if (i < that.data.img_arr.length) {//若图片还没有传完，则继续调用函数  
                    that.up(cb)
                }else{
                    console.log("多图处理完毕");
                    return typeof cb == "function" && cb()
                }

            }
        })
    },
    /*
     发布商机
     */
    fabu:function (e) {
        var that = this

        var title = e.detail.value.title
        if(!title){
            tips.error("请输入 标题")
            return false
        }
        var content = e.detail.value.content
        if(!content){
            tips.error("请输入 内容")
            return false
        }

        var user = wx.getStorageSync('user')
        var data = {
            uid:user.userid,
            title:title
        }
        http.req("/Api/shangji_title_check",data,function (res) {
            if(res.code==223){
                if (that.data.img_arr.length > 0) {
                    that.up(function(){
                            console.log(that.data)
                            
                            var type = that.data.leixingindex
                          
                            if(type<1){
                                tips.error("请选择 类型")
                                return false
                            }
                            var hangyeindex = that.data.hangyeindex
                            var hangye = that.data.info.cate_list[hangyeindex].id
                            var quan_list = that.data.info_list
                            var quan_arr = []
                            for(var i=0;i<quan_list.length;i++){
                                if(quan_list[i].select ==true){
                                   quan_arr.push(quan_list[i].q_id)
                                }
                            }
                            var quan = quan_arr.join(',');
                            var user = wx.getStorageSync('user')
                            var data = {
                                uid:user.userid,
                                q_id:quan,
                                title:title,
                                intro:content,
                                type:type,
                                category:hangye,
                                pic1:that.data.pic1,
                                pic2:that.data.pic2,
                                pic3:that.data.pic3,
                            }
                            console.log(data)
                            http.req("/Api/add_shangji",data,function (res) {
                                 console.log(res)
                                if(res.code==200){
                                    tips.ok(res.msg_val)
                                    tips.redirectTo('/pages/main/shangji/shangji')
                                }else{
                                    tips.error(res.msg_val)
                                }
                            })

                    })

                }else{
                    console.log(that.data)
                   
                    var type = that.data.leixingindex
                  
                    if(type<1){
                        tips.error("请选择 类型")
                        return false
                    }
                    var hangyeindex = that.data.hangyeindex
                    var hangye = that.data.info.cate_list[hangyeindex].id
                    var quan_list = that.data.info_list
                    var quan_arr = []
                    for(var i=0;i<quan_list.length;i++){
                        if(quan_list[i].select ==true){
                           quan_arr.push(quan_list[i].q_id)
                        }
                    }
                    var quan = quan_arr.join(',');
                    var user = wx.getStorageSync('user')
                    var data = {
                        uid:user.userid,
                        q_id:quan,
                        title:title,
                        intro:content,
                        type:type,
                        category:hangye
                    }
                    console.log(data)
                    http.req("/Api/add_shangji",data,function (res) {
                        //console.log(res)
                        if(res.code==200){
                            tips.ok(res.msg_val)
                            tips.redirectTo('/pages/main/shangji/shangji')
                        }else{
                            tips.error(res.msg_val)
                        }
                    })
                }
             /* 标题不重复结束*/
            }else if(res.code==438){
                tips.error('标题重复了')
            }
        })
        
        
    }

})
